class AcquisitionMethodsController < ApplicationController

  before_filter :get_museum

  def index
    @acquisition_methods = AcquisitionMethod.find_by_sql(["SELECT acquisition_methods.*, count(entities.id) as entities_count FROM acquisition_methods join entities on entities.acquisition_method_id = acquisition_methods.id where entities.acquisition_method_id IS NOT NULL and entities.museum_id = ? group by acquisition_methods.id", @museum.id])

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @acquisition_methods }
    end
  end

  # GET /acquisition_methods/1
  # GET /acquisition_methods/1.xml
  def show
    @acquisition_method = AcquisitionMethod.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @acquisition_method }
    end
  end

  # GET /acquisition_methods/new
  # GET /acquisition_methods/new.xml
  def new
    @acquisition_method = AcquisitionMethod.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @acquisition_method }
    end
  end

  # GET /acquisition_methods/1/edit
  def edit
    @acquisition_method = AcquisitionMethod.find(params[:id])
  end

  # POST /acquisition_methods
  # POST /acquisition_methods.xml
  def create
    @acquisition_method = AcquisitionMethod.new(params[:acquisition_method])

    respond_to do |format|
      if @acquisition_method.save
        format.html { redirect_to museum_acquisition_method_path(@museum, @acquisition_method) }
        format.xml  { render :xml => @acquisition_method, :status => :created, :location => @acquisition_method }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @acquisition_method.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /acquisition_methods/1
  # PUT /acquisition_methods/1.xml
  def update
    @acquisition_method = AcquisitionMethod.find(params[:id])

    respond_to do |format|
      if @acquisition_method.update_attributes(params[:acquisition_method])
        flash[:notice] = 'AcquisitionMethod was successfully updated.'
        format.html { redirect_to(museum_acquisition_method_path(@museum, @acquisition_method)) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @acquisition_method.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /acquisition_methods/1
  # DELETE /acquisition_methods/1.xml
  def destroy
    @acquisition_method = AcquisitionMethod.find(params[:id])
    @acquisition_method.destroy

    respond_to do |format|
      format.html { redirect_to(acquisition_methods_url) }
      format.xml  { head :ok }
    end
  end
  
  private
  
  def get_museum
    @museum = Museum.find(params[:museum_id])
  end  
  
end
